package com.lagou.edu.controller;

import com.lagou.edu.service.AuthCodeService;
import com.lagou.edu.service.EmailFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/code")
public class AuthCodeController {

    @Autowired
    private AuthCodeService authCodeService;

    @Autowired
    private EmailFeignClient emailFeignClient;



    @GetMapping("/create/{email}")
    public Boolean existsEmailSendCode(@PathVariable("email") String emailaddress){
         if(authCodeService.canCreateEmail(emailaddress)&&!authCodeService.existsCode(emailaddress)){
             // 生成验证码
             String code = authCodeService.getCode();
             // 发送验证码邮件
             emailFeignClient.sendEmail(emailaddress, code);
             System.out.println("验证码为：" + code);
             authCodeService.addAuthCode(emailaddress, code);
             return true;
         }else {
             return false;
         }
    }

    @GetMapping("/test")
    public String test(){
        return "success";
    }
}
